package cn.maszy.action.projectmanagement;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.stripes.action.SessionScope;

import org.springframework.beans.factory.annotation.Autowired;

import cn.maszy.common.BaseAction;
import cn.maszy.common.DateTools;
import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.common.Transcoding;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineeringMapper;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategory;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerMaterial;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerPosition;
import cn.maszy.model.projectmanagement.B_T_Ufp_Engineering;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineeringExample;
import cn.maszy.model.systemsetting.B_T_Ufp_Login;
import cn.maszy.model.systemsetting.B_T_Ufp_Report;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineerCategoryService;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineerMaterialService;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineerPositionService;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineeringService;
import cn.maszy.service.systemsetting.B_T_Ufp_LoginService;

@SessionScope
public class EngineeringAction extends BaseAction<B_T_Ufp_Engineering> {
	/**
	 * 版本控制信息
	 */
	@Autowired
	private B_T_Ufp_EngineeringMapper b_T_Ufp_EngineeringMapper;
	private static final long serialVersionUID = 6179604301571294802L;
	//新建的一个对象用来存放list；用于导出画面
	private List<B_T_Ufp_Report> b_T_Ufp_ReportList;
	private List<B_T_Ufp_Login> list_loginname;
	private B_T_Ufp_Report b_T_Ufp_Report;
	private B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategory;
	private List<B_T_Ufp_EngineerCategory> list_b_T_Ufp_EngineerCategory;
	private B_T_Ufp_EngineerPosition b_T_Ufp_EngineerPosition;
	private List<B_T_Ufp_EngineerPosition> list_b_T_Ufp_EngineerPosition;
	private List<B_T_Ufp_EngineerMaterial> list_b_T_Ufp_EngineerMaterial;
	private B_T_Ufp_Engineering b_T_Ufp_Engineering;
	@Autowired
	private B_T_Ufp_LoginService B_T_Ufp_LoginService;
	@Autowired
	private B_T_Ufp_EngineerCategoryService b_T_Ufp_EngineerCategoryService;
	@Autowired
	private B_T_Ufp_EngineerPositionService b_T_Ufp_EngineerPositionService;
	@Autowired
	private B_T_Ufp_EngineerMaterialService b_T_Ufp_EngineerMaterialService;
	@Autowired
	private B_T_Ufp_EngineeringService b_T_Ufp_EngineeringService;
	private String checkIds;
	private String status;
	private String flag;
	//附加说明
	private String remarknum[];
	//直接费
	private float total;
	//管理费
	private float managetotal;
	//税金
	private float taxes;
	//工程总金额
	private float all;
	//材料条数
	private int num;
	//材料总计
	private float materialtotal;
	private String level;
	private String strattime;
	private String endtime;
	/*******************STRUTS ACTION METHOD*************************/
	public String engineeringDelete() throws Exception {
		int i=b_T_Ufp_EngineeringService.delete(checkIds);
		if(i>0)
		return ajaxForwardSuccess(getText("msg.operation.success"), "engineeringList","", "forward", "projectmanagement/Engineering_engineeringList");
		return ajaxForwardError(getText("msg.operation.failure"));
	}
	public String engineeringUpdate() throws Exception {
		int i=b_T_Ufp_EngineeringService.update(b_T_Ufp_Engineering);
		if(i>0)
//			if(b_T_Ufp_Engineering.getStatus().equals("0"))
//			{
				return ajaxForwardSuccess(getText("msg.operation.success"), "engineeringList","", "forward", "projectmanagement/Engineering_engineeringList");
//			}
//			else
//			{
//				return ajaxForwardSuccess(getText("msg.operation.success"), "materialListH","", "closeCurrent", "systemsetting/Material_materialList?status="+b_T_Ufp_Engineering.getStatus());
//			}
		return ajaxForwardError(getText("msg.operation.failure"));
	}
	public String engineeringList() throws Exception {
		//如果当前用户的权限是 管理员
		String level =request.getSession().getAttribute("level").toString();
		if("1".equals(level))
		{
			pager = new Pager<B_T_Ufp_Engineering>(pageNum, numPerPage,orderField,orderDirection);
			pager= b_T_Ufp_EngineeringService.selectList(b_T_Ufp_Engineering,pager,strattime,endtime);
			list_loginname = B_T_Ufp_LoginService.selectListchange();
			return SUCCESS;
		}
		if(b_T_Ufp_Engineering ==null)
		{
			b_T_Ufp_Engineering =new B_T_Ufp_Engineering();
		}
		String realname =SessionFactory.getSessionUserRealNam();
		b_T_Ufp_Engineering.setCreateUser(realname);
		pager = new Pager<B_T_Ufp_Engineering>(pageNum, numPerPage,orderField,orderDirection);
		pager= b_T_Ufp_EngineeringService.selectList(b_T_Ufp_Engineering,pager,strattime,endtime);
		list_loginname = B_T_Ufp_LoginService.selectListchange();
		return SUCCESS;
	}
	public String engineeringDetail() throws Exception {
		level = request.getSession().getAttribute("level").toString();
		if(b_T_Ufp_Engineering==null){
			b_T_Ufp_Engineering=b_T_Ufp_EngineeringService.selectOne("");
			b_T_Ufp_Engineering=new B_T_Ufp_Engineering();
			//自动生成工程编码
			//查询共有多少条数据
			B_T_Ufp_EngineeringExample example=new B_T_Ufp_EngineeringExample();
			int i=b_T_Ufp_EngineeringService.count(example, b_T_Ufp_EngineeringMapper)+1;
			String code ="GC-00"+i;
			b_T_Ufp_Engineering.setEngineeringNum(code);
			b_T_Ufp_Engineering.setDesignerName(SessionFactory.getSessionUserRealNam());
			b_T_Ufp_Engineering.setBudgetTime(DateTools.getServerDateTime(9));
			//designerUuid作为附加说明用
			b_T_Ufp_Engineering.setDesignerUuid("1.以上报价工程量以实际发生量为准"+System.getProperty("line.separator")+"2.此报价不含五金,洁具,墙地砖,玻璃,灯具,开关插座,地板。");
		}else{
			b_T_Ufp_Engineering=b_T_Ufp_EngineeringService.selectOne(b_T_Ufp_Engineering.getUuid());
		}
		return SUCCESS;
	}
	public String engineeringReport() throws Exception {
		String uuid ="";
		if(flag !=null&&!"".equals(flag))
		{
			b_T_Ufp_EngineerPosition = new B_T_Ufp_EngineerPosition();
			//由于页面超链接 为写 target="navTab" 编码会变。
			flag =Transcoding.getcode(request, flag);
//			flag =new String(flag.getBytes("ISO8859_1"),"UTF-8");
			String flag1[]=flag.split(",");
			uuid=flag1[0];
			b_T_Ufp_EngineerPosition.setEngineeringUuid(flag1[0]);
			b_T_Ufp_EngineerPosition.setEngineeringType(flag1[1]);
		}
		//取得工程基本信息
		b_T_Ufp_Engineering= b_T_Ufp_EngineeringService.selectOne(uuid);
		//取得附加说明并进行设置；
		String remark= b_T_Ufp_Engineering.getDesignerUuid();
		remarknum = remark.split("\r\n");
		//取得该工程的装修部位
		list_b_T_Ufp_EngineerPosition = b_T_Ufp_EngineerPositionService.select(b_T_Ufp_EngineerPosition);
		//取得属于各个部位的项目
		b_T_Ufp_ReportList = new ArrayList<B_T_Ufp_Report>();
		for(int i =0;i<list_b_T_Ufp_EngineerPosition.size();i++)
		{	
			b_T_Ufp_Report=new B_T_Ufp_Report();
			
			//将查询到的部位数据放入事先定义好的实体类中；
			b_T_Ufp_Report.setB_T_Ufp_EngineerPosition(list_b_T_Ufp_EngineerPosition.get(i));
			//创建b_T_Ufp_EngineerCategory对象。并设置用来查询属于本部位的数据
			b_T_Ufp_EngineerCategory = new B_T_Ufp_EngineerCategory();
			b_T_Ufp_EngineerCategory.setPositionUuid(list_b_T_Ufp_EngineerPosition.get(i).getUuid());
			//取得positionuuid等于部位表uuid的数据
			list_b_T_Ufp_EngineerCategory= b_T_Ufp_EngineerCategoryService.select(b_T_Ufp_EngineerCategory);
			
			///将查询到的部位数据放入事先定义好的实体类中
			b_T_Ufp_Report.setB_T_Ufp_EngineerCategoryList(list_b_T_Ufp_EngineerCategory);
			//设置部位name和id用来查询本部位的总计金额
			b_T_Ufp_EngineerCategory.setPositionUuid(list_b_T_Ufp_EngineerPosition.get(i).getUuid());
			b_T_Ufp_EngineerCategory.setPositionName(list_b_T_Ufp_EngineerPosition.get(i).getPositionName());
			float f = b_T_Ufp_EngineerCategoryService.totalamount(b_T_Ufp_EngineerCategory);
			
			///将查询到的部位数据放入事先定义好的实体类中
			b_T_Ufp_Report.setSumPirce(f);
			//将获得的数据放入list；
			b_T_Ufp_ReportList.add(b_T_Ufp_Report);
		}
		//获取工程uuid 算出直接费合计
		String  engineeringuuid =b_T_Ufp_Engineering.getUuid();
		B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategoryall= new B_T_Ufp_EngineerCategory();
		b_T_Ufp_EngineerCategoryall.setEngineeringUuid(engineeringuuid);
		total = b_T_Ufp_EngineerCategoryService.totalall(b_T_Ufp_EngineerCategoryall);
		total= (float)(Math.round(total*100))/100;
		if(!"".equals(b_T_Ufp_Engineering.getDiscount())&&b_T_Ufp_Engineering.getDiscount()!=null)
		{
//			total= total *b_T_Ufp_Engineering.getDiscount();
			total= total *(b_T_Ufp_Engineering.getDiscount()/10);
			total= (float)(Math.round(total*100))/100;
		}
		//管理费
		if(!"".equals(b_T_Ufp_Engineering.getManagementFree())&&b_T_Ufp_Engineering.getManagementFree()!=null)
		{
			//managetotal =total*b_T_Ufp_Engineering.getManagementFree();
			managetotal =total*(b_T_Ufp_Engineering.getManagementFree()/100);
			managetotal= (float)(Math.round(managetotal*100))/100;
		}
		else
		{
			managetotal=0.0f;
		}
		//税金合计
		if(!"".equals(b_T_Ufp_Engineering.getTaxes())&&b_T_Ufp_Engineering.getTaxes()!=null)
		{
			//taxes = total*b_T_Ufp_Engineering.getTaxes();
			taxes = total*(b_T_Ufp_Engineering.getTaxes()/100);
			taxes= (float)(Math.round(taxes*100))/100;
		}
		else
		{
			taxes=0.0f;
		}
		//根据工程uuid 取得自购材料总计
		B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterialall = new B_T_Ufp_EngineerMaterial();
		b_T_Ufp_EngineerMaterialall.setEngineeringUuid(engineeringuuid);
//		float material =b_T_Ufp_EngineerMaterialService.totalamount(b_T_Ufp_EngineerMaterialall);
		//工程总金额
//		all = material+total+taxes+managetotal;
		all = total+taxes+managetotal;
		all= (float)(Math.round(all*100))/100;
		//由于客户需求材料和报价要分开
//		//根据工程id查询自购材料信息；
//		list_b_T_Ufp_EngineerMaterial=b_T_Ufp_EngineerMaterialService.select(b_T_Ufp_EngineerMaterialall);
//		//取得材料的条数
//		num =list_b_T_Ufp_EngineerMaterial.size()+1;
//		//算出材料的总和
//		materialtotal =b_T_Ufp_EngineerMaterialService.totalamount(b_T_Ufp_EngineerMaterialall);
		return SUCCESS;
	}
	public String engineeringReportAll() throws Exception {
		String uuid ="";
		if(flag !=null&&!"".equals(flag))
		{
			b_T_Ufp_EngineerPosition = new B_T_Ufp_EngineerPosition();
			flag =Transcoding.getcode(request, flag);
			String flag1[]=flag.split(",");
			uuid=flag1[0];
			b_T_Ufp_EngineerPosition.setEngineeringUuid(flag1[0]);
			b_T_Ufp_EngineerPosition.setEngineeringType(flag1[1]);
		}
		//取得工程基本信息
		b_T_Ufp_Engineering= b_T_Ufp_EngineeringService.selectOne(uuid);
		//取得附加说明并进行设置；
		String remark= b_T_Ufp_Engineering.getDesignerUuid();
		remarknum = remark.split("\r\n");
		//取得该工程的装修部位
		list_b_T_Ufp_EngineerPosition = b_T_Ufp_EngineerPositionService.select(b_T_Ufp_EngineerPosition);
		//取得属于各个部位的项目
		b_T_Ufp_ReportList = new ArrayList<B_T_Ufp_Report>();
		for(int i =0;i<list_b_T_Ufp_EngineerPosition.size();i++)
		{	
			b_T_Ufp_Report=new B_T_Ufp_Report();
			
			//将查询到的部位数据放入事先定义好的实体类中；
			b_T_Ufp_Report.setB_T_Ufp_EngineerPosition(list_b_T_Ufp_EngineerPosition.get(i));
			//创建b_T_Ufp_EngineerCategory对象。并设置用来查询属于本部位的数据
			b_T_Ufp_EngineerCategory = new B_T_Ufp_EngineerCategory();
			b_T_Ufp_EngineerCategory.setPositionUuid(list_b_T_Ufp_EngineerPosition.get(i).getUuid());
			//取得positionuuid等于部位表uuid的数据
			list_b_T_Ufp_EngineerCategory= b_T_Ufp_EngineerCategoryService.select(b_T_Ufp_EngineerCategory);
			
			///将查询到的部位数据放入事先定义好的实体类中
			b_T_Ufp_Report.setB_T_Ufp_EngineerCategoryList(list_b_T_Ufp_EngineerCategory);
			//设置部位name和id用来查询本部位的总计金额
			b_T_Ufp_EngineerCategory.setPositionUuid(list_b_T_Ufp_EngineerPosition.get(i).getUuid());
			b_T_Ufp_EngineerCategory.setPositionName(list_b_T_Ufp_EngineerPosition.get(i).getPositionName());
			float f = b_T_Ufp_EngineerCategoryService.totalamount(b_T_Ufp_EngineerCategory);
			
			///将查询到的部位数据放入事先定义好的实体类中
			b_T_Ufp_Report.setSumPirce(f);
			//将获得的数据放入list；
			b_T_Ufp_ReportList.add(b_T_Ufp_Report);
		}
		//获取工程uuid 算出直接费合计
		String  engineeringuuid =b_T_Ufp_Engineering.getUuid();
		B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategoryall= new B_T_Ufp_EngineerCategory();
		b_T_Ufp_EngineerCategoryall.setEngineeringUuid(engineeringuuid);
		total = b_T_Ufp_EngineerCategoryService.totalall(b_T_Ufp_EngineerCategoryall);
		total= (float)(Math.round(total*100))/100;
		if(!"".equals(b_T_Ufp_Engineering.getDiscount())&&b_T_Ufp_Engineering.getDiscount()!=null)
		{
//			total= total *b_T_Ufp_Engineering.getDiscount();
			total= total *(b_T_Ufp_Engineering.getDiscount()/10);
			total= (float)(Math.round(total*100))/100;
		}
		//管理费
		if(!"".equals(b_T_Ufp_Engineering.getManagementFree())&&b_T_Ufp_Engineering.getManagementFree()!=null)
		{
			//managetotal =total*b_T_Ufp_Engineering.getManagementFree();
			managetotal =total*(b_T_Ufp_Engineering.getManagementFree()/100);
			managetotal= (float)(Math.round(managetotal*100))/100;
		}
		else
		{
			managetotal=0.0f;
		}
		//税金合计
		if(!"".equals(b_T_Ufp_Engineering.getTaxes())&&b_T_Ufp_Engineering.getTaxes()!=null)
		{
			//taxes = total*b_T_Ufp_Engineering.getTaxes();
			taxes = total*(b_T_Ufp_Engineering.getTaxes()/100);
			taxes= (float)(Math.round(taxes*100))/100;
		}
		else
		{
			taxes=0.0f;
		}
		//根据工程uuid 取得自购材料总计
		B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterialall = new B_T_Ufp_EngineerMaterial();
		b_T_Ufp_EngineerMaterialall.setEngineeringUuid(engineeringuuid);
//		float material =b_T_Ufp_EngineerMaterialService.totalamount(b_T_Ufp_EngineerMaterialall);
		//工程总金额
//		all = material+total+taxes+managetotal;
		all = total+taxes+managetotal;
		all= (float)(Math.round(all*100))/100;
		//由于客户需求材料和报价打印要分开
//		//根据工程id查询自购材料信息；
//		list_b_T_Ufp_EngineerMaterial=b_T_Ufp_EngineerMaterialService.select(b_T_Ufp_EngineerMaterialall);
//		//取得材料的条数
//		num =list_b_T_Ufp_EngineerMaterial.size()+1;
//		//算出材料的总和
//		materialtotal =b_T_Ufp_EngineerMaterialService.totalamount(b_T_Ufp_EngineerMaterialall);
		return SUCCESS;
	}
	
	
	public String materialReport() throws Exception {
		String uuid ="";
		if(flag !=null&&!"".equals(flag))
		{
			flag =Transcoding.getcode(request, flag);
			//由于页面超链接 为写 target="navTab" 编码会变。
//			flag =new String(flag.getBytes("ISO8859_1"),"UTF-8");
			String flag1[]=flag.split(",");
			uuid=flag1[0];
		}
		//取得工程基本信息
		b_T_Ufp_Engineering= b_T_Ufp_EngineeringService.selectOne(uuid);
		//获取工程uuid 算出直接费合计
		String  engineeringuuid =b_T_Ufp_Engineering.getUuid();
		//根据工程uuid 取得自购材料总计
		B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterialall = new B_T_Ufp_EngineerMaterial();
		b_T_Ufp_EngineerMaterialall.setEngineeringUuid(engineeringuuid);
		//根据工程id查询自购材料信息；
		list_b_T_Ufp_EngineerMaterial=b_T_Ufp_EngineerMaterialService.select(b_T_Ufp_EngineerMaterialall);
		//取得材料的条数
		num =list_b_T_Ufp_EngineerMaterial.size()+1;
		//算出材料的总和
		materialtotal =b_T_Ufp_EngineerMaterialService.totalamount(b_T_Ufp_EngineerMaterialall);
		return SUCCESS;
	}
/********************* class set/get ****************************/
	
	public B_T_Ufp_Engineering getB_T_Ufp_Engineering() {
		return b_T_Ufp_Engineering;
	}

	public void setB_T_Ufp_Engineering(B_T_Ufp_Engineering b_T_Ufp_Engineering) {
		this.b_T_Ufp_Engineering = b_T_Ufp_Engineering;
	}
	
	public String getCheckIds() {
		return checkIds;
	}

	public void setCheckIds(String checkIds) {
		this.checkIds = checkIds;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	public B_T_Ufp_EngineerPosition getB_T_Ufp_EngineerPosition() {
		return b_T_Ufp_EngineerPosition;
	}
	public void setB_T_Ufp_EngineerPosition(
			B_T_Ufp_EngineerPosition b_T_Ufp_EngineerPosition) {
		this.b_T_Ufp_EngineerPosition = b_T_Ufp_EngineerPosition;
	}
	public List<B_T_Ufp_EngineerPosition> getList_b_T_Ufp_EngineerPosition() {
		return list_b_T_Ufp_EngineerPosition;
	}
	public void setList_b_T_Ufp_EngineerPosition(
			List<B_T_Ufp_EngineerPosition> list_b_T_Ufp_EngineerPosition) {
		this.list_b_T_Ufp_EngineerPosition = list_b_T_Ufp_EngineerPosition;
	}
	public B_T_Ufp_EngineerCategory getB_T_Ufp_EngineerCategory() {
		return b_T_Ufp_EngineerCategory;
	}
	public void setB_T_Ufp_EngineerCategory(
			B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategory) {
		this.b_T_Ufp_EngineerCategory = b_T_Ufp_EngineerCategory;
	}
	public List<B_T_Ufp_EngineerCategory> getList_b_T_Ufp_EngineerCategory() {
		return list_b_T_Ufp_EngineerCategory;
	}
	public void setList_b_T_Ufp_EngineerCategory(
			List<B_T_Ufp_EngineerCategory> list_b_T_Ufp_EngineerCategory) {
		this.list_b_T_Ufp_EngineerCategory = list_b_T_Ufp_EngineerCategory;
	}
	public List<B_T_Ufp_Report> getB_T_Ufp_ReportList() {
		return b_T_Ufp_ReportList;
	}
	public void setB_T_Ufp_ReportList(List<B_T_Ufp_Report> b_T_Ufp_ReportList) {
		this.b_T_Ufp_ReportList = b_T_Ufp_ReportList;
	}
	public B_T_Ufp_Report getB_T_Ufp_Report() {
		return b_T_Ufp_Report;
	}
	public void setB_T_Ufp_Report(B_T_Ufp_Report b_T_Ufp_Report) {
		this.b_T_Ufp_Report = b_T_Ufp_Report;
	}
	public float getTotal() {
		return total;
	}
	public void setTotal(float total) {
		this.total = total;
	}
	public float getManagetotal() {
		return managetotal;
	}
	public void setManagetotal(float managetotal) {
		this.managetotal = managetotal;
	}
	public float getTaxes() {
		return taxes;
	}
	public void setTaxes(float taxes) {
		this.taxes = taxes;
	}
	public float getAll() {
		return all;
	}
	public void setAll(float all) {
		this.all = all;
	}
	public List<B_T_Ufp_EngineerMaterial> getList_b_T_Ufp_EngineerMaterial() {
		return list_b_T_Ufp_EngineerMaterial;
	}
	public void setList_b_T_Ufp_EngineerMaterial(
			List<B_T_Ufp_EngineerMaterial> list_b_T_Ufp_EngineerMaterial) {
		this.list_b_T_Ufp_EngineerMaterial = list_b_T_Ufp_EngineerMaterial;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public float getMaterialtotal() {
		return materialtotal;
	}
	public void setMaterialtotal(float materialtotal) {
		this.materialtotal = materialtotal;
	}
	public String getLevel() {
		return level;
	}
	public void setLevel(String level) {
		this.level = level;
	}
	public String[] getRemarknum() {
		return remarknum;
	}
	public void setRemarknum(String[] remarknum) {
		this.remarknum = remarknum;
	}
	public List<B_T_Ufp_Login> getList_loginname() {
		return list_loginname;
	}
	public void setList_loginname(List<B_T_Ufp_Login> list_loginname) {
		this.list_loginname = list_loginname;
	}
	public String getStrattime() {
		return strattime;
	}
	public void setStrattime(String strattime) {
		this.strattime = strattime;
	}
	public String getEndtime() {
		return endtime;
	}
	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}


}
